<?php
/**
 * 《供应商》实现类
 *
 * @author 孙晓晔
 * @version $Id: Supplier.class.php 1059 2010-04-08 07:55:21Z sunxy $
 */
require_once(dirname(__FILE__) . '/ContactUtil.class.php');

class Supplier extends ContactUtil {
	public $tbl = Constant::tbl_supplier;
	
	public $info = '2';
	
	var $aa = 0;
	var $prefix = "supplier.";
	
	public $company = array(
		'-' => '请选择',
		'1' => '供应商(采购)',
		'2' => '产品工厂(工程)',
		'3' => '货运公司(物流)',
		
		'9' => '其它',
	);
	
	public $share = array(
		'-' => '请选择',
		'1' => '共享',
		'2' => '不共享',
		
		'9' => '其它',
	);
	
	public $pay = array(
		'-' => '请选择',
		'预付20%' => '预付20%',
		'预付30%' => '预付30%',
		'预付50%' => '预付50%',

		'预付全款' => '预付全款',			
		'货到付款' => '货到付款',
	
		'当月结算' => '当月结算',
		'月结30天' => '月结30天',
		'月结60天' => '月结60天',
		'月结90天' => '月结90天',		
	);

	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
		'star' => '等级',
		
		'category' => '大类',
		'type' => '小类',
			
		'status' => '状态',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'category' => '大类',
		'type' => '小类',
		'name' => '名称',
		'emp' => '负责人',
			
		'status' => '状态',
	
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$category = $this->_category();
		$controller->assign_by_ref('category_option', $category);
		
		$controller->assign_by_ref('share_option', $this->share);
		$controller->assign_by_ref('company_option', $this->company);
		$controller->assign_by_ref('currency_option', $this->currency);
		$controller->assign_by_ref('pay_option', $this->pay);
	}
	
	function tab_id($controller, $request) {
		$data = array(
			'员工信息' => array('cc'=>'emp.php?do=Emp.view_id', 'js'=>''),
			'客户信息' => array('cc'=>'contact.php?do=Customer.view_code', 'js'=>'1'),
			'产品信息' => array('cc'=>'mold.php?do=Mold.view_code', 'js'=>'1'),
			'供应商' => array('cc'=>'contact.php?do=Supplier.view_id', 'js'=>'1'),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	function view_id($controller, $request) {
		$this->pretreat($controller);
	
		$controller->display($request, 'contact/supplier/id');
		return true;
	}
	
	function view_idPost($controller, $request) {
	
		$id = $this->_input($request['supplier_s']);
	
		$row = $this->_view($id);
	
		if (!$this->_view_id_my($row)) {
			$controller->assign('msg', "对不起，操作受限，请联系管理员。");
				
			$controller->display($request, 'info');
			return true;
		}

		$row = $this->_view($row['id']);

		$controller->assign_by_ref('result', $row);

		$ret = $this->_view_tab();

		$controller->assign_by_ref('tabs', $ret);

		$controller->display($request, 'contact/supplier/view_id');
	
		return true;
	}
	
	function _view_id_my($data) {
	
		if ($this->_is_admin()) {
			return true;
		}
	
		if ($this->_audit('25') || $this->_audit('76')) {
			return true;
		}
	
		$emp = $_SESSION['id'];
	
		if ($data['emp'] == $emp || $data['engineer'] == $emp || $data['oper'] == $emp) {
			return true;
		}
	
		return false;
	}
	
	function _view_tab() {
		$data = array(
			'联系人' => array('cc'=>'info.php?do=Contact.view_supplier&id=', 'js'=>'1', 'v'=>'1'),
			'银行帐号' => array('cc'=>'contact.php?do=SupplierBank.view_supplier&id=', 'js'=>'1', 'v'=>'1'),
			
			'付款信息' => array('cc'=>'finance.php?do=Payment.view_supplier&id=', 'js'=>'1'),
			'支出信息' => array('cc'=>'finance.php?do=Out.view_supplier&id=', 'js'=>'1'),
			'产品信息' => array('cc'=>'mold.php?do=Mold.view_supplier&id=', 'js'=>'1'),
				
			'采购信息' => array('cc'=>'material.php?do=Purchase.view_supplier&id=', 'js'=>'1'),
			'货运信息' => array('cc'=>'express.php?do=Plan.view_supplier&id=', 'js'=>'1'),
			'外发信息' => array('cc'=>'mold.php?do=Process.view_supplier&id=', 'js'=>'1'),
				
			'附件信息' => array('cc'=>'contact.php?do=Supplier.view_file&id=', 'js'=>'1'),
		);
	
		return $this->_tab2($data);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$star_op = array(
			'<' => '小于',
			'<=' => '小于或等于',
			'=' => '等于',
			'>' => '大于',
			'>=' => '大于或等于',
		);
		$controller->assign('star_op', $star_op);
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '负责人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'contact/supplier/find');
		return true;
	}
	
	function _category() {
		$key = $this->prefix . "category_";
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		if ($this->aa > 0) {
			$fmt = "SELECT DISTINCT category FROM %s WHERE company = '%s' AND length(category) > 0";
			$sql = sprintf($fmt, $this->tbl, $this->aa);
		} else {
			$fmt = "SELECT DISTINCT category FROM %s WHERE length(category) > 0";
			$sql = sprintf($fmt, $this->tbl);
		}		
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['category']] = $row['category'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function _type() {
		$key = $this->prefix . "type_";
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		if ($this->aa > 0) {
			$fmt = "SELECT DISTINCT type FROM %s WHERE company = '%s' AND length(type) > 0";
			$sql = sprintf($fmt, $this->tbl, $this->aa);
		} else {
			$fmt = "SELECT DISTINCT type FROM %s WHERE length(type) > 0";
			$sql = sprintf($fmt, $this->tbl);
		}
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['type']] = $row['type'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category($controller, $request) {
		if (!empty($request['category'])) {
			$category = $request['category'];
		}

		if (!empty($request['category_s'])) {
			$category = $request['category_s'];
		}

		if (!empty($request['category_t'])) {
			$category = $request['category_t'];
		}

		if (empty($category) || $category == '-') {
			$data[] = array('code'=>'-', 'name'=>'请选择');
			echo json_encode($data);
			return false;
		}

		$key = $this->prefix . "category_" . substr(md5($category), 0, 10);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				echo json_encode($ret);
				return true;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT type FROM %s WHERE category = '%s'";
		$sql = sprintf($fmt, $this->tbl, $category);
		// debug($sql);
		$result = $db->query($sql);
		$ret[] = array('code'=>'-', 'name'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$row['code'] = $row['type'];
			$row['name'] = $row['type'];

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		echo json_encode($ret);
		return true;
	}

	/**
	 * 清除缓存
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category_type($controller, $request) {
		$category = $this->_category();
		while (list($k, $v) = each($category)) {
			$key = $this->prefix . "category_" . substr(md5($k), 0, 10);
			clean($key);
		}

		$key = $this->prefix . "category_";
		clean($key);

		$key = $this->prefix . "type_";
		clean($key);

		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		$share_s = $request['share_s'];
		
		$cert_type_s = $request['cert_type_s'];

		$category_s = $request['category_s'];
		$type_s = $request['type_s'];
		
		$star_s = $request['star_s'];
		$star_op_s = $request['star_op_s'];
		if (empty($star_op_s)) {
			$star_op_s = "=";
		}
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($name_s)) {
			$s = "%" . $name_s . "%";
			$where .= sprintf(" AND (name LIKE '%s' OR fullname LIKE '%s' OR jj LIKE '%s' OR contact LIKE '%s')", $s, $s, $s, $s);
		}
		
		if (!empty($star_s) && $star_s <> '-') {
			$where .= sprintf(" AND star %s '%s'", $star_op_s, $star_s);
		}

		if (!empty($share_s) && $share_s <> '-') {
			$where .= " AND share = '" . $share_s . "'";
		}
		
		if (!empty($cert_type_s) && $cert_type_s <> '-') {
			$where .= " AND cert_type = '" . $cert_type_s . "'";
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		if ($this->_audit('149') || $this->_audit('150')) {
			return 'visible = 1 ';
		}

		$emp = $_SESSION['id'];

		return sprintf("visible = 1 AND (share = '1' OR emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['contact_'] = $this->_num_contact($row['id']);
				$row['bank_'] = $this->_num_bank($row['id']);								

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);
				
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'contact/supplier/list');
		return true;
	}
	
	function _num_contact($id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->num($this->info, $id);
	}
	
	function _num_bank($id) {
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$aa = new Bank();
		
		return $aa->num($this->info, $id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'contact/supplier/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		// $request['id_s'] = $this-> _insert($controller, $request);
		
		$name = addslashes($request['name']);
		$fullname = addslashes($request['fullname']);
		
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);
		
		$cert_type = $request['cert_type'];
		$cert_no = trim($request['cert_no']);
		
		$address = addslashes($request['address']);
		$url = strtolower($request['url']);
		$jj = addslashes($request['jj']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, name, zjm, fullname, cert_type, cert_no, address, url, jj, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $zjm, $fullname, $cert_type, $cert_no, $address, $url, $jj, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert($controller, $request) {
		$company = $request['company'];
		$code = $request['code'];
		$name = addslashes($request['name']);

		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);

		$fullname = addslashes($request['fullname']);
		$category = addslashes($request['category']);
		$type = addslashes($request['type']);

		$province = $request['province'];
		$city = $request['city'];
		$district = $request['district'];

		$address = addslashes($request['address']);

		$url = strtolower($request['url']);
		$share = strtolower($request['share']);
		
		$jj = addslashes($request['jj']);
		
		$content = addslashes($request['content']);
		
		$pay = addslashes($request['pay']);
		$currency = $request['currency'];
		$tax = $request['tax'];
		
		$emp = $this->_input($request['emp']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, zjm, fullname, category, type, address, url, share, jj, content, pay, currency, tax, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $zjm, $fullname, $category, $type, $address, $url, $share, $jj, $content, $pay, $currency, $tax, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		$id = $db->insert_id();

		$contact = stripslashes($request['contact']);
		$this->_contact($id, $contact);
		
		return $id;
	}

	function _contact1($info, $info_id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_linkman_primary($info, $info_id);
	}

	function _contact($id, $contact) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_insert($this->info, $id, $contact);
	}
	
	function _contact_x($id, $contact) {
		debug($contact);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET contact = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $contact, $id);
		debug($sql);
		$db->query($sql);
	}

	function _contact_id($id, $contact) {
		$this->_contact_x($id, $contact);
		
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_update2($this->info, $id, $contact);
	}

	function _delete($z) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_delete($z);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('contact', $this->_linkman($id));

		$controller->display($request, 'contact/supplier/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$company = $request['company'];
		$code = $request['code'];
		$name = addslashes($request['name']);

		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);

		$fullname = addslashes($request['fullname']);
		
		$cert_type = $request['cert_type'];
		$cert_no = trim($request['cert_no']);
		
		$star = $request['star'];
		$category = addslashes($request['category']);
		$type = addslashes($request['type']);

		$province = $request['province'];
		$city = $request['city'];
		$district = $request['district'];

		$address = addslashes($request['address']);
		$geo = strtolower($request['geo']);

		$url = strtolower($request['url']);
		$share = addslashes($request['share']);
		$jj = addslashes($request['jj']);

		$content = addslashes($request['content']);
		
		$pay = addslashes($request['pay']);
		$currency = $request['currency'];
		$tax = $request['tax'];

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', name = '%s', zjm = '%s', fullname = '%s', cert_type = '%s', cert_no = '%s', category = '%s', type = '%s', address = '%s', geo = '%s', url = '%s', ";
		$fmt .= "star = '%s', share = '%s', jj = '%s', content = '%s', pay = '%s', currency = '%s', tax = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $zjm, $fullname, $cert_type, $cert_no, $category, $type, $address, $geo, $url, $star, $share, $jj, $content, $pay, $currency, $tax, $emp, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		} else {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		}

		$contact = stripslashes($request['contact']);
		$this->_contact_id($id, $contact);

		$z = stripslashes($request['z']);
		$this->_delete($z);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('contact', $this->_linkman($id));

		$controller->display($request, 'contact/supplier/copy');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);
		
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'contact/supplier/upload');
		return true;
	}

	function contact($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'contact/supplier/contact');
		return true;
	}

	function contactPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'contact/supplier/contact');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		// $controller->assign_by_ref('contact', $this->_linkman($id));		
		
		// $controller->assign_by_ref('bank', $this->_bank($id));
		$ret = $this->_view_tab();
		
		$controller->assign_by_ref('tabs', $ret);
		
		if (!empty($request['small'])) {
			$controller->display($request, 'contact/supplier/view_small');
			return true;
		}

		$controller->display($request, 'contact/supplier/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row['company_t'] = $this->company[$row['company']];
		$row['cert_type_t'] = $this->cert_type[$row['cert_type']];
		
		$row['c'] = $this->_contact1($this->info, $row['id']);

		$row['pct'] = $row['province'] . $row['city'] . $row['district'];
		
		$row['tax'] = floatval($row['tax']);
		
		// $row['files'] = $this->_files($row['id']);

		$row = $this->_view_t($row);

		return $row;
	}

	function _linkman($id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		$ret = $aa->_ajax_linkman($this->info, $id);
		
		return $this->_shift($ret);
	}
	
	function _bank($id) {
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$aa = new Bank();
		
		$ret = $aa->_ajax_bank($this->info, $id);
		
		return $this->_shift($ret);
	}
	
	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
	
	function ajax_id($controller, $request) {
		$id = $this->_input($request['id']);
		
		$row = $this->id($this->tbl, $id);
		$row['tax'] = floatval($row['tax']);
		
		echo json_encode($row);
	}

	function ajax_zjm($controller, $request) {
		
		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}
	
	function _zjm_clean($s) {
		if (empty($s)) {
			return false;
		}
	
		$s = strtolower($s);
		for ($i = 1; $i <= strlen($s); $i++) {
			$q = substr($s, 0, $i);
			$key = sprintf("%s_%s_%s", $this->zjm, $this->tbl, $q);
				
			debug($key);
			clean($key);
		}
	
		return true;
	}

	function _ajax_zjm($request) {
		$q = strtolower($request['q']);
		
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		if (preg_match("/^(\d+)(.+)/", $q, $m)) {
			$status = $m[1];
			$zjm = $m[2];
		} else {
			$status = 1;
			$zjm = $q;
		}

		if (empty($zjm)) {
			debug("OK" . $zjm);
			return array();
		}
		
		// $key = $this->prefix . "ajax_zjm_" . $q;
		if ($status == 1) {
			$key = sprintf("%s_%s_%s_%s", $this->zjm, $this->tbl, $this->prefix, $zjm);
		} else {
			$key = sprintf("%s_%s_%s_%s_%s", $this->zjm, $this->tbl, $this->prefix, $status, $zjm);
		}
		
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		if ($this->aa > 0) {
			$fmt = "SELECT *, length(zjm) c FROM %s WHERE visible = 1 AND company = '%s' AND status = '%s' AND zjm LIKE '%s' ORDER BY c, id DESC LIMIT %s";
			$sql = sprintf($fmt, $this->tbl, $this->aa, $status, "%" . $zjm . "%", $n);
		} elseif ($status == '1') {
			$fmt = "SELECT *, length(zjm) c FROM %s WHERE visible = 1 AND (share = '1' OR emp = '%s') AND status IN ('1', '9') AND zjm LIKE '%s' ORDER BY c, id DESC LIMIT %s";
			$sql = sprintf($fmt, $this->tbl, $_SESSION['id'], "%" . $zjm . "%", $n);
		} else {
			$fmt = "SELECT *, length(zjm) c FROM %s WHERE visible = 1 AND (share = '1' OR emp = '%s') AND status = '%s' AND zjm LIKE '%s' ORDER BY c, id DESC LIMIT %s";
			$sql = sprintf($fmt, $this->tbl, $_SESSION['id'], $status, "%" . $zjm . "%", $n);
		}
		
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			// $row['dept_t'] = Name::dept($row['dept']);

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}
	
	function z2($controller, $request) {
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		// $fmt = "SELECT * FROM %s LIMIT 1";
		$sql = sprintf($fmt, $this->tbl, $s);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
				
			$a = $this->_linkman($row['id']);
				
			// print_r($a);
				
			$b = $this->_abc($a);
				
			// print_r($b);
				
			// $c = json_encode($b);
			
			arrayRecursive($b, 'urlencode', true);
			$json = json_encode($b);
			$c = urldecode($json);
				
			$fmt = "UPDATE %s SET contact = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $c, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
				
			echo ".";
		}
	
		logdate($controller, $request);
		return true;
	}
	
	
	
	function _abc($data) {
	
		$ret = array();
		$a = array();
		while (list($k, $v) = each($data)) {
				
			$a['name'] = $v['name'];
			$a['job'] = $v['job'];
			$a['mobile'] = $v['mobile'];
			$a['dh'] = $v['dh'];
			$a['tel'] = $v['tel'];
			$a['fax'] = $v['fax'];
			$a['email'] = $v['email'];
				
			$a['address'] = $v['address'];
			$a['qq'] = $v['qq'];
			$a['skype'] = $v['skype'];
			$a['content'] = $v['content'];
				
			$ret[] = $a;
		}
	
		return $ret;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		$emp = $_SESSION['id'];
		
		if ($row['emp'] == $emp) {
			return true;
		}
		
		if ($row['oper'] == $emp) {
			return true;
		}

		return false;
	}

	function _delete2($id) {
		$db = newdb($this->dsn_w);

		$fmt = "DELETE FROM %s WHERE info = '%s' AND info_id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_contact, $this->info, $id);
		// debug($sql);
		$db->query($sql);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-100 day'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'contact/supplier/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function t($controller, $request) {
		
		$s = "abc";
		$key = $this->prefix . "ajax_zjm_" . $q;
		for ($i = 1; $i <= strlen($s); $i++) {
			$q = substr($s, 0, $i);
			$a = sprintf("%sajax_zjm_%s", $this->prefix, $q);
			echo $a . "\n"; 
		}

		logdate($controller, $request);
		return true;
	}

	function _t_supplier($data) {
		$code = $data['code'];
		$name = addslashes($data['name']);

		$zjm = $data['zjm'];

		$fullname = addslashes($data['fullname']);
		$category = $data['category'];

		$province = $data['province'];
		$city = $data['city'];
		$district = $data['district'];

		$address = addslashes($data['address']);

		$url = strtolower($data['url']);
		$jj = addslashes($data['jj']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(code, name, zjm, fullname, category, province, city, district, address, url, jj";
		$fmt .= ", createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s'";
		$fmt .= ", '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $code, $name, $zjm, $fullname, $category, $province, $city, $district, $address, $url, $jj);
		// debug($sql);
		$db->query($sql);

		$id = $db->insert_id();
			
		$fmt = "SELECT * FROM %s WHERE info = '2' AND info_id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_contact, $data['id']);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$this->_t_contact($row, $id);
			echo "C";
		}
	}

	function _t_contact($data, $id) {
		$name = addslashes($data['name']);
		$tel = addslashes($data['tel']);
		$fax = addslashes($data['fax']);
		$mobile = addslashes($data['mobile']);
		$email = addslashes($data['email']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(info, info_id, name, tel, fax, mobile, email, createtime, modifytime) VALUES('%s', '%s', '%s', ";
		$fmt .= "'%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, Constant::tbl_contact, $this->info, $id, $name, $tel, $fax, $mobile, $email);
		// debug($sql);
		$db->query($sql);
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email('302');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '供应商资料';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');

		$body = $this->_tj_html($controller, $request, 'contact/supplier/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email('302');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = '供应商资料（周报）';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);

		$body = $this->_tj_html($controller, $request, 'contact/supplier/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'emp';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function _x($id) {
		
	}
	
	function audit($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
		
		if ($this->_is_admin()) {
			$ret['status'] = 1;
		} else if ($this->_audit('11')) {
			$ret['status'] = 1;
		} else if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} else if ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}

		echo json_encode($ret);
	}
	
	function _update_x($supplier, $supplier_x, $payment, $payment_x) {
		if (empty($supplier)) {
			return $this->_update_x_a($supplier_x, $payment, $payment_x);
		}
		
		$a = json_decode($supplier_x);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE name LIKE '%s'";
		$sql = sprintf($fmt, $this->tbl, '%' . $a->name . '%');
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return $this->_update_x_b($info_id, $payment, $payment_x);
		}
		
		return $this->_update_x_a($supplier_x, $payment, $payment_x);
	}
	
	function _update_x_a($supplier_x, $payment, $payment_x) {
		$a = json_decode($supplier_x);
		
		$name = addslashes($a->name);
		$fullname = addslashes($a->fullname);
		$address = addslashes($a->address);
		$url = $a->web;
		
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, zjm, fullname, address, url, emp, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $zjm, $fullname, $address, $url, $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$info_id = $db->insert_id();
		
		return $this->_update_x_b($info_id, $payment, $payment_x);
	}	
	
	function _update_x_b($info_id, $payment, $payment_x) {
		$info = '2';
		debug($info_id);
		if ($payment == '1' || $payment == '3') {
			$this->_update_linkman($info, $info_id, $payment_x);
		}
		
		if ($payment == '2' || $payment == '4') {
			$this->_update_bank($info, $info_id, $payment_x);
		}
		
		return $info_id;
	}
	
	function _update_linkman($info, $info_id, $payment_x) {
		$json = json_decode($payment_x);
			
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$contact = new Contact();
		$contact->_update($info, $info_id, $json);
	}
	
	function _update_bank($info, $info_id, $payment_x) {
		$json = json_decode($payment_x);
			
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		$bank->_update($info, $info_id, $json);
	}
	
	function ajax_exist1($controller, $request) {
		$name = $request['name'];
		if (empty($name)) {
			echo "true";
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE name = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}
	
	function ajax_exist2($controller, $request) {
		$fullname = $request['fullname'];
		if (empty($fullname)) {
			echo "true";
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE fullname = '%s'";
		$sql = sprintf($fmt, $this->tbl, $fullname);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}
	
	function _star($id, $star) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET star = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $star, $id);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function c($controller, $request) {
		$s = "createtime > DATE_ADD(now(), INTERVAL -6 MONTH)";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE visible = '1' AND %s";
		$sql = sprintf($fmt, $this->tbl, $s);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($this->_imperfect($this->info, $row['id'])) {
				$this->_c_notice($controller, $request, $row['id']);
			}
		}

		logdate($controller, $request);
		return true;
	}
	
	function _c_notice($controller, $request, $id) {		
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);		
		$controller->assign_by_ref('contact', $this->_linkman($id));
		
		$from = '101';
		$to = $row['emp'];
		// $to = '101';
		$subject = sprintf('供应商 %s 信息不完善', $id);
		$body = $this->_tj_html($controller, $request, 'contact/supplier/body_x');
		// echo $body;
					
		$this->_notice($from, $to, $subject, $body, 1);
		
		if ($row['emp'] <> $row['oper']) {
			$this->_notice($from, $row['oper'], $subject, $body, 1);
		}
	}
	
	function _imperfect($info, $info_id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_imperfect($info, $info_id);
	}
	
	function abcd($controller, $request) {
		$dd = date('Y-m-d', strtotime("-6 MONTH"));
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status < 3 AND createtime < '%s'";
		$sql = sprintf($fmt, $this->tbl, $dd);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($row['share'] == '2') {
				continue;
			}
			
			$status = $this->_status($row['id']);
			if ($row['status'] == $status) {
				continue;
			}			
	
			$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $status, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function _status($id) {
		require_once(dirname(__FILE__) . '/../finance/Payment.class.php');
		$aa = new Payment();
		if ($aa->_supplier($id)) {
			return "1";
		}
		
		require_once(dirname(__FILE__) . '/../finance/Out.class.php');
		$aa = new Out();
		if ($aa->_supplier($id)) {
			return "1";
		}
		
		require_once(dirname(__FILE__) . '/../material/Purchase.class.php');
		$aa = new Purchase();
		if ($aa->_supplier($id)) {
			return "1";
		}
		
		require_once(dirname(__FILE__) . '/../express/Plan.class.php');
		$aa = new Plan();
		if ($aa->_supplier($id)) {
			return "1";
		}
		
		return "2";
	}
	
	function bank($controller, $request) {
		
		$id = $request['id'];
		
		$a = $this->_linkman($id);
		$name = array('-'=>'请选择');
		while (list($k, $v) = each($a)) {
			$name[$v['name']] = $v['name'];
		}
		
		$controller->assign_by_ref('name_option', $name);
		
		$controller->assign_by_ref('supplier', $this->_view($id));
		
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		
		$bank->supplier($controller, $request);
	}
	
	function bankPost($controller, $request) {
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		
		$bank->supplierPost($controller, $request);
		
		$this->bank($controller, $request);
	}
	
	function ajax_bank($controller, $request) {
		$name = $request['name'];
		$code = $request['code'];
		
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		
		$ret = array('status'=>'1');
		if ($bank->_name_code($name, $code)) {
			$ret['status'] = '2';
		}
		
		echo json_encode($ret);
	}
	
	function merge($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'contact/supplier/merge');
		return true;
	}
	
	function mergePost($controller, $request) {
		
		$from = $request['id'];
		$to = $this->_input($request['to']);
		
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
		
		$bank->merge('2', $from, $to);
		
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$contact = new Contact();
		
		$contact->merge('2', $from, $to);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'contact/supplier/_list');
		return true;
	}
}
?>